<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.util.*" %>


<html>
<head>
<title>MySQL Database creation</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<body>

<jsp:declaration>

Statement stmt;
Connection con;
String url = "jdbc:mysql://213.180.89.116:3306/ht13";

</jsp:declaration>
<table>
<%
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, "ht13", "projekt");

stmt = con.createStatement();
//string var1 = request.getParameter("seqId");
ResultSet result;
//if("0" == "0") {
    //result = stmt.executeQuery("SELECT * FROM reading WHERE time >= DATE_SUB( NOW(), INTERVAL 1 DAY)");
//} else {
    //result = stmt.executeQuery("SELECT * FROM reading WHERE id >= " request.getParameter("seqId"));
//}
result = stmt.executeQuery("SELECT * FROM reading");

List<String> list = new ArrayList<String>();

while(result.next())
          {
             list.add(result.getString("id"));
             list.add(result.getString("pulse"));
             list.add(result.getString("o2"));
             list.add(result.getString("time"));
             list.add(result.getString("quality"));
          } 

con.close();

int id = 0;

Iterator<String> it = list.iterator();
boolean first = true;
out.print("[");

while (it.hasNext()) {
    if(!first) {
        out.print(",");
        first = false;
    }
    out.print("{");
    for (int i = 0; i < 5 && it.hasNext(); i++) {
        out.print("\"seqNr\": " + it.next() + ",");
        out.print("\"pulse\": " + it.next() +",");
        out.print("\"o2\": " + it.next() +",");
        out.print("\"time\": " + it.next() +",");
        out.print("\"quality\": " + it.next());
    }
    out.print("}");
}
out.print("]");
%>
</table>
</body>
</html>